/*
	This script file is part of the Sonic 3 A.I.R. script software distribution.
	Copyright (C) 2017-2024 by Eukaryot

	Published under the GNU GPLv3 open source software license, see license.txt
	or https://www.gnu.org/licenses/gpl-3.0.en.html
*/



//-------------------------------------------------------//
// Effect of leaves from the ground                      //
//-------------------------------------------------------//

//# address-hook(0x03da00) end(0x03da22)
function void MHZLeavesEffect.Update()
{
	#if STANDALONE
		if (debug_mode.state)
			return
	#endif

	if (u8[0xfffff7c0] < 0x10)
	{
		A2 = 0xffffb000
		A3 = A0 + 0x2e
		MHZLeavesEffect.UpdateForPlayer()

		A2 = 0xffffb04a
		if (s8[A2 + 0x04] < 0)
		{
			A3 = A0 + 0x30
			MHZLeavesEffect.UpdateForPlayer()
		}
	}
}


//# address-hook(0x03da24) end(0x03db4e)
function void MHZLeavesEffect.UpdateForPlayer()
{
	if (u8[A2 + 0x46] != 0x0c)
		return

	if (u8[A2 + 0x2a] & char.flag.IN_AIR)
	{
		u16[A3] = u16[A2 + 0x1a]
		return
	}

	GetRandomNumber()
	D0.u16 &= 0x03
	if (D0.u16 != 0)
		return

	D2.u16 = u16[A3]
	if (D2.u16 != 0)
	{
		u16[A3] = 0
		if ((u8[A2 + 0x2a] & char.flag.ON_OBJECT) == 0 && D2.u16 >= 0x0400)
		{
			D2.u16 = 0x0100
			A4 = 0x03db50
			D6 = 5
			while (D6.s16 >= 0)
			{
				if (allocDynamicObjectAfterA0())
				{
					fn03db68()

					objA1.render_flags |= render_flag.WORLD
					objA1.sprite_priority = 0
					objA1.box_size.x = 0x04
					objA1.box_size.y = 0x04
					objA1.position.x.u16 = u16[A2 + 0x10]
					objA1.position.y.u16 = u16[A2 + 0x14] + 0x18

					GetRandomNumber()
					u16[A1 + 0x26] = D0.u16
					D0.u16 &= 0x01ff
					D0.u16 += u16[(A4+=2)-2] - 0x0100
					objA1.velocity.x = D0.u16
					D0 = (D0 << 16) + (D0 >> 16)
					D0.u16 &= 0xff
					D0.u16 += D2.u16 - u16[(A4+=2)-2]
					D0.s16 = -D0.s16
					objA1.velocity.y = D0.u16
					D0 = (D0 << 16) + (D0 >> 16)
					D0.u16 &= 0x03
					D0.u16 += 0x02
					u16[A1 + 0x34] = D0.u16
					objA1.update_address = addressof(MHZSingleLeaf.Type3)
					++u8[0xfffff7c0]
				}

				--D6.s16
			}
			return
		}
	}

	D2.u16 = abs(s16[A2 + 0x18])
	if (D2.u16 >= 0x0500)
	{
		if (allocDynamicObjectAfterA0())
		{
			fn03db68()

			objA1.render_flags |= render_flag.WORLD
			objA1.sprite_priority = 0
			objA1.box_size.x = 0x04
			objA1.box_size.y = 0x04
			objA1.position.x.u16 = u16[A2 + 0x10]
			objA1.position.y.u16 = u16[A2 + 0x14] + 0x10
			D2.u16 = (D2.u16 - 0x0500) >> 4
			D2.u16 += 0x0200
			D2.s16 = -D2.s16
			objA1.velocity.y = D2.u16
			D0 = (D0 << 16) + (D0 >> 16)
			D0.u16 &= 0x03
			D0.u16 += 0x02
			u16[A1 + 0x34] = D0.u16
			objA1.update_address = addressof(MHZSingleLeaf.Type1)
			++u8[0xfffff7c0]
		}
	}
}


//# address-hook(0x03db68) end(0x03dbae)
function void fn03db68()
{
	if (u8[0xfffff7c1] == 0)
	{
		objA1.mapping_offset = 0x03dc5c
		objA1.sprite_attributes = (sprite_attribute.PRIORITY | sprite_attribute.PALETTE.LINE3 | 0x0368)
		return
	}

	D0.u16 = u8[A0 + 0x32] & 0x07
	++u8[A0 + 0x32]
	D0.u8 = u8[0x03dbb0 + D0.u16]
	if (D0.u8 == 0)
	{
		objA1.mapping_offset = 0x03dc74
		objA1.sprite_attributes = (sprite_attribute.PRIORITY | sprite_attribute.PALETTE.LINE3 | 0x0363)
	}
	else
	{
		objA1.mapping_offset = 0x03dc5c
		objA1.sprite_attributes = (sprite_attribute.PRIORITY | sprite_attribute.PALETTE.LINE3 | 0x0363)
	}
}


//# address-hook(0x03dbb8) end(0x03dbda)
function void MHZSingleLeaf.Type1()
{
	UpdateMovementStraightSimple()
	objA0.velocity.y += u16[A0 + 0x34] * 4
	if (objA0.velocity.y >= 0)
	{
		objA0.update_address = addressof(MHZSingleLeaf.Type2)
		objA0.value26 = level.framecounter.low
	}

	MHZSingleLeaf.SharedUpdate()
	DrawObject()
}


//# address-hook(0x03dbe0) end(0x03dc12)
function void MHZSingleLeaf.Type2()
{
	D0.u8 = objA0.value26
	objA0.value26 += 4
	LookupSinCos()
	objA0.velocity.x = D0.u16
	UpdateMovementStraightSimple()
	objA0.velocity.y += u16[A0 + 0x34]

	if ((objA0.render_flags & render_flag.VISIBLE) == 0)
	{
		objA0.position.x.u16 = 0x7f00
		--u8[0xfffff7c0]
	}

	MHZSingleLeaf.SharedUpdate()
	DrawOrUnloadObject()
}


//# address-hook(0x03dc18) end(0x03dc34)
function void MHZSingleLeaf.Type3()
{
	UpdateMovementStraightSimple()
	D0.u16 = u16[A0 + 0x34] * 4
	objA0.velocity.y += D0.u16
	if (objA0.velocity.y >= 0)
	{
		objA0.update_address = addressof(MHZSingleLeaf.Type2)
	}

	MHZSingleLeaf.SharedUpdate()
	DrawObject()
}


//# address-hook(0x03dc3a) end(0x03dc5a)
function void MHZSingleLeaf.SharedUpdate()
{
	--objA0.animation.timer
	if (s8(objA0.animation.timer) < 0)
	{
		objA0.animation.timer = 0x07
		++objA0.animation.sprite
		objA0.animation.sprite &= 0x01
		if (objA0.velocity.x < 0)
			objA0.animation.sprite += 2
	}
}
